<?php
/**
 * Created by PhpStorm.
 * User: huliren<huliren0516@163.com>
 * Date: 2017/8/31
 * Time: 19:40
 */
define("SITE_AUTH", 'advertising-line-share');

require '../../../common.inc.php';

use lib\core\AdminAction;
use Center\WeightShareConfig as Model;
use Common\PhotoImage;
use Common\Bridge;

class share extends AdminAction
{

    private $list_url = 'data/modules/share/share.php';
    private static $types = [
        1 => '打卡分享配置',
        2 => '排行分享配置',
    ];

    public function doDefault()
    {
        $type = intval($_GET['type']);
        if (empty($type)) {
            $type = 1;
        }
        $smarty = $this->app->page();
        $where = ['Type' => $type];
        $pdo = Bridge::pdo(Bridge::DB_CENTER_R);
        $data = $pdo->find('WeightShareConfig', $where, '*', 'Sort DESC,Id DESC');
        if ($data) {
            $smarty->value('list', $data);
        }
        $smarty->value('type', $type);
        $smarty->value('types', self::$types);
        $smarty->output('share/share_list.tpl');
    }

    public function doEdit()
    {
        $smarty = $this->app->page();
        $id = intval(trim($_GET['id']));
        $type = intval($_GET['type']);
        if (empty($type)) {
            $type = 1;
        }
        if ($id < 0) {
            HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
        }
        if (!empty($id)) {
            $data = Model::getDataInfoById($id);
            if (empty($data)) {
                HDshowMsg('数据不存在！', ['type' => 'error', 'url' => $this->list_url]);
            }
            $type = $data['Type'];
            $smarty->value('info', $data);
        }
        $smarty->value('type', $type);
        $smarty->value('types', self::$types);
        $smarty->output('share/share_edit.tpl');
    }

    public function doUpdata()
    {
        $id = (isset($_POST['id']) and $_POST['id'] !== '') ? $_POST['id'] : null;
        $sort = intval(trim($_POST['sort']));
        $sort = ($sort and $sort !== '') ? $sort : 0;
        $bg = $_FILES['gb'];
        $ac = $_FILES['ac'];
        $type = intval($_POST['Type']);

        if ($id !== null) {
            $data = [
                'Sort' => $sort,
                'Type' => $type,
            ];
            if (isset($bg['name']) && $bg['error'] == 0 && $bg['size'] > 0) {
                $bg_rel = PhotoImage::checkImage($bg, 0, 0, 0);
                $bg_url = Model::saveBgUrl($bg_rel['tmp_name']);
                $data = array_merge($data, ['BgUrl' => $bg_url]);
            }
            if (isset($ac['name']) && $ac['error'] == 0 && $ac['size'] > 0) {
                $ac_rel = PhotoImage::checkImage($ac, 0, 0, 0);
                $ac_url = Model::saveActUrl($ac_rel['tmp_name']);
                $data = array_merge($data, ['ActUrl' => $ac_url]);
            }

            $rel = Model::update($id, $data);
        } else {
            if (isset($bg['name']) && $bg['error'] == 0 && $bg['size'] > 0) {
                $bg_rel = PhotoImage::checkImage($bg, 0, 0, 0);
                $bg_url = Model::saveBgUrl($bg_rel['tmp_name']);
            } else {
                $bg_url = "";
            }
            if (isset($ac['name']) && $ac['error'] == 0 && $ac['size'] > 0) {
                $ac_rel = PhotoImage::checkImage($ac, 0, 0, 0);
                $ac_url = Model::saveActUrl($ac_rel['tmp_name']);
            } else {
                $ac_url = "";
            }

            $data = [
                'ActUrl' => $ac_url,
                'BgUrl' => $bg_url,
                'Sort' => $sort,
                'Type' => $type,
            ];

            $rel = Model::add($data);
        }
        if ($rel) {
            HDshowMsg('添加成功！', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('添加失败，联系管理员！', ['type' => 'error', 'url' => $this->list_url]);
        }
    }

    public function doDelete()
    {
        $id = $_GET['id'];
        $rel = Model::deleteData($id);
        \Common\Bridge::memcache()->delete('WEIGHT_SHARE_CONFIG_LIST');
        if ($rel) {
            HDshowMsg('删除成功！', ['type' => 'success', 'url' => $this->list_url]);
        } else {
            HDshowMsg('删除失败，联系管理员！', ['type' => 'success', 'url' => $this->list_url]);
        }
    }

}

$app->run();